package com.sunda.spmsweb.wmscontroller;


import com.alibaba.fastjson.JSONObject;
import com.sunda.spmscommon.ResponseResult;
import com.sunda.spmsweb.util.JWTUtil;
import com.sunda.spmswms.service.ISapDeliveryNoteLogService;
import com.sunda.spmswms.service.IWhsDumpDtlService;
import com.sunda.spmswms.service.IWhsDumpService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 转储任务表 前端控制器
 * </p>
 *
 * @author Wayne
 * @since 2021-06-29
 */
@RestController
@RequestMapping("/whsDump")
@Api(tags = "转储任务接口", description = "转储任务接口")
public class WhsDumpController {

    @Autowired
    IWhsDumpService iWhsDumpService;

    @Autowired
    IWhsDumpDtlService iWhsDumpDtlService;

    @Autowired
    ISapDeliveryNoteLogService iSapDeliveryNoteLogService;

    @RequestMapping("/getWhsDumpList")
    @ApiOperation(value = "获取一个转储任务表头", notes = "获取转储任务表头\n" +
            "示例参数，whsDumpUuid = c5e31bbb127a129ce0530101007f83b6 ", httpMethod = "POST")
    public ResponseResult getWhsDumpList(String whsDumpUuid){
        try {
            return ResponseResult.success().add("whsDumpList", iWhsDumpService.getWhsDumpByUuid(whsDumpUuid));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/getWhsDumpListPage")
    @ApiOperation(value = "获取转储任务表头分页", notes = "获取转储任务表头分页\n" +
            "示例参数\n" +
            "{\n" +
            "\t\"beginDate\": \"2021-08-10\",\n" +
            "\t\"endDate\": \"2021-08-14\",\n" +
            "\t\"whsDumpNo\": \"\",\n" +
            "\t\"goodsType\": \"\",\n" +
            "\t\"werksDumpOut\": \"\",\n" +
            "\t\"whsLocationCodeDumpOut\": \"\",\n" +
            "\t\"spmsStatus\": \"\",\n" +
            "\t\"sapDumpOutNo\": \"\",\n" +
            "\t\"sapDeliveryNote\": \"\",\n" +
            "\t\"werksDumpIn\": \"\",\n" +
            "\t\"whsLocationCodeDumpIn\": \"\",\n" +
            "\t\"pageNo\": 1,\n" +
            "\t\"pageSize\": 20\n" +
            "}", httpMethod = "POST")
    public ResponseResult getWhsDumpListPage(@RequestBody JSONObject doc){
        try {
            return ResponseResult.success().add("whsDumpList", iWhsDumpService.getWhsDumpListPage(doc));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }


    @RequestMapping("/getWhsDumpDtl")
    @ApiOperation(value = "获取转储任务明细", notes = "获取转储任务明细\n" +
            "示例参数，whsDumpUuid = f7d9abf4e3b5455098879c93bde770b4 ", httpMethod = "POST")
    public ResponseResult getWhsMoveList(String whsDumpUuid){
        try {
            return ResponseResult.success().add("whsDumpDtl", iWhsDumpDtlService.getWhsDumpDtlList(whsDumpUuid));
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/saveWhsDump")
    @ApiOperation(value = "创建转储任务及明细", notes = "创建转储任务及明细\n" +
            "示例参数如下，当转储任务uuid字段为空时表示新建转储任务，uuid不为空表示更新\n" +
            "{\n" +
            "    \"whsDumpDtl\": [\n" +
            "      {\n" +
            "        \"actualInStorage\": \"[]\",\n" +
            "        \"actualOutQty\": 1,\n" +
            "        \"item\": null,\n" +
            "        \"materialNo\": null,\n" +
            "        \"basicUnit\": null,\n" +
            "        \"dumpInRemark\": null,\n" +
            "        \"rowId\": 1,\n" +
            "        \"whsDumpUuid\": \"\",\n" +
            "        \"boxNote\": \"BJ-1180114864-0102\",\n" +
            "        \"dumpOutRemark\": null,\n" +
            "        \"materialZhDesc\": null,\n" +
            "        \"materialEnDesc\": null,\n" +
            "        \"plannedOutQty\": 1,\n" +
            "        \"plannedOutStorage\": null,\n" +
            "        \"actualOutStorage\": \"[{\\\"storageUuid\\\":\\\"f17e13b4d9ad4d39ba55548eff544391\\\",\\\"qty\\\":1,\\\"storageNo\\\":\\\"C010101\\\"}]\",\n" +
            "        \"actualInQty\": null\n" +
            "      }\n" +
            "    ],\n" +
            "    \"whsDump\": {\n" +
            "      \"whsDumpUuid\": \"\",\n" +
            "      \"whsDumpNo\": \"\",\n" +
            "      \"goodsType\": \"X\",\n" +
            "      \"businessType\": \"01\",\n" +
            "      \"werksDumpOut\": \"CN01\",\n" +
            "      \"whsLocationCodeDumpOut\": \"1061\",\n" +
            "      \"dumpOutRemark\": \"转出备注\",\n" +
            "      \"createUserid\": \"999947\",\n" +
            "      \"createDate\": \"2021-08-11\",\n" +
            "      \"dumpOutOperator\": null,\n" +
            "      \"dumpOutDate\": null,\n" +
            "      \"spmsStatus\": \"1\",\n" +
            "      \"dumpOutSapDate\": null,\n" +
            "      \"sapDumpOutNo\": null,\n" +
            "      \"sapDeliveryNote\": null,\n" +
            "      \"sapWhsOutNo\": null,\n" +
            "      \"werksDumpIn\": \"KF01\",\n" +
            "      \"whsLocationCodeDumpIn\": \"1006\",\n" +
            "      \"dumpInRemark\": \"转入备注\",\n" +
            "      \"dumpInOperator\": null,\n" +
            "      \"dumpInDate\": null,\n" +
            "      \"sapWhsInNo\": null,\n" +
            "      \"supplierCode\": null,\n" +
            "      \"voucherType\": null,\n" +
            "      \"purchaseGroup\": null,\n" +
            "      \"purchaseOrg\": null,\n" +
            "      \"returnMark\": null,\n" +
            "      \"remark\": null,\n" +
            "      \"dumpInSapDate\": null,\n" +
            "      \"deliveryDate\": null,\n" +
            "      \"currentStatus\":\"1\",\n" +
            "      \"targetStatus\":\"3\"\n" +
            "    }\n" +
            "  }", httpMethod = "POST")
    public ResponseResult saveWhsDump(@RequestBody JSONObject doc){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iWhsDumpService.saveWhsDump(doc, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/whsDumpOutToSap")
    @ApiOperation(value = "转储任务提交SAP", notes = "转储任务提交SAP\n" +
            "0删除；1草稿；2转出可执行；3转出完成；4提交SAP获取PO成功；5提交SAP获取PO失败；6提交SAP获取DN成功；7提交SAP获取DN失败；8转出已撤销；\n" +
            "第一次提交SAP前状态为：3、5\n" +
            "\t成功后状态：4\n" +
            "\t失败后状态：5\n" +
            "\n" +
            "第二次提交SAP前状态为：4、7\n" +
            "\t成功后状态：6，更新库存计算\n" +
            "\t失败后状态：7\n" +
            "\n" +
            "撤销前状态为：6\n" +
            "\t成功后状态：8，更新库存计算\n" +
            "\t失败忽略状态更新\n" +
            "\n" +
            "1,2,3,5 —— 删除\n" +
            "1,2,5 —— 保存\n" +
            "2 —— 提交SPMS\n" +
            "3,5 —— 提交SAP获取PO\n" +
            "4,7 —— 提交SAP获取DN\n" +
            "4,6,7 —— 从SAP撤销\n" +
            "8\n" +
            "示例参数，whsDumpUuid = ef1cf3d2a4ad4fb4a79284a52f32f1c8\n" +
            "postingDate 在获取DN时必输", httpMethod = "POST")
    public ResponseResult whsDumpOutToSap(String whsDumpUuid, String dumpOutSapDate){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iWhsDumpService.whsDumpOutToSap(whsDumpUuid, dumpOutSapDate, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/deleteWhsDump")
    @ApiOperation(value = "删除转储任务", notes = "删除转储任务\n" +
            "示例参数，whsDumpUuid = c5e31bbb127a129ce0530101007f83b6 ", httpMethod = "POST")
    public ResponseResult deleteWhsDump(String whsDumpUuid){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iWhsDumpService.deleteWhsDump(whsDumpUuid, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }

    @RequestMapping("/cancelWhsDump")
    @ApiOperation(value = "撤销转储任务", notes = "撤销转储任务\n" +
            "示例参数，whsDumpUuid = 288f2546c7954c90ba3afa3a8146487b；cancelPostingDate = 2021-07-01", httpMethod = "POST")
    public ResponseResult cancelWhsDump(String whsDumpUuid, String cancelPostingDate){
        try {
            String userId = JWTUtil.getUserId(SecurityUtils.getSubject().getPrincipal().toString());
            return iWhsDumpService.cancelWhsDump(whsDumpUuid, cancelPostingDate, userId);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseResult.error("请求数据失败").add("error", e.getMessage());
        }
    }
}
